/**
 * 
 */
package com.srini.servlets;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.srini.pojos.LoggedInUser;
import com.srini.pojos.Role;
import com.srini.pojos.UserInfo;
import com.vssr.persist.DataBaseWriter;

/**
 * @author A200171
 * 
 */
public class AddUserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		super.doGet(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		System.out.println("Adding user....");

		String empName = req.getParameter("empName");
		String email = req.getParameter("email");
		String travelAllowance = req.getParameter("trallowance");
		String foodAllowance = req.getParameter("foodallowance");
		String role = req.getParameter("role");
		String managerId = req.getParameter("managerId");

		LoggedInUser user = new LoggedInUser();
		user.setUserName(empName);
		user.setPassword(empName);

		user.setTrAllowance(travelAllowance);
		user.setFoodAllowance(foodAllowance);
		user.setCreatedOn(Calendar.getInstance().getTime().toString());
		if (managerId != null) {
			user.setManagerId(Integer.valueOf(managerId));
		}

		UserInfo userInfo = new UserInfo();
		userInfo.setEmail(email);
		user.setUserInfo(userInfo);
		Role roleObj = new Role();
		roleObj.setRole(role);

		user.setRole(roleObj);
		DataBaseWriter.save(user);

		req.setAttribute("message", "User successfully created.");
		req.setAttribute("currentPage", "admin");
		req.getRequestDispatcher("/main.jsp").forward(req, resp);
	}
}
